package com.tesco.controller;

import com.tesco.entity.domin.YgGoods;
import com.tesco.entity.domin.YgShoppingcart;
import com.tesco.entity.vo.ShoppingCartVo;
import com.tesco.service.YgGoodsService;
import com.tesco.service.YgShoppingcartService;
import com.tesco.utils.TokenUtils;
import com.tesco.utils.WebApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:jc
 * @Date:2022/1/26 19:53
 * @Description:com.tesco.controller
 */
@Api(tags = "购物车")
@RequestMapping("/ygshoppingcart")
@RestController
@CrossOrigin(origins = "*")
public class ShoppingCartController {

    @Autowired
    private YgShoppingcartService ygShoppingcartService;

    @Autowired
    private YgGoodsService ygGoodsService;

    @PostMapping("/addShoppingCart")
    @ApiOperation("添加购物车数据(token)")
    public WebApiResult addShoppingCart(HttpServletRequest request,@ApiParam(name = "spid",value = "商品id") @RequestParam("spid") Integer spid){
        try {
            ygShoppingcartService.insertShoppingCart(spid, TokenUtils.getUsername(request));
            return WebApiResult.ok("添加购物车成功");
        } catch (Exception e) {
            e.printStackTrace();
            return WebApiResult.error("添加购物车失败");
        }
    }

    @PostMapping("/shoppingCartList")
    @ApiOperation("购物车列表(token)")
    public WebApiResult<List<ShoppingCartVo>> shoppingCartList(HttpServletRequest request){
        try {
            String username = TokenUtils.getUsername(request);
            List<YgShoppingcart> ygShoppingCarts = ygShoppingcartService.shoppingCartList(username);
            List<ShoppingCartVo> shoppingCartVos = new ArrayList<>();
            for (YgShoppingcart ygShoppingCart : ygShoppingCarts) {
                ShoppingCartVo shoppingCartVo = new ShoppingCartVo();
                YgGoods ygGoods = ygGoodsService.goodInfo(ygShoppingCart.getSpid());
                BeanUtils.copyProperties(ygGoods,shoppingCartVo);
                shoppingCartVo.setGwid(ygShoppingCart.getGwid());
                shoppingCartVo.setCreatedat(ygShoppingCart.getCreatedat());
                shoppingCartVos.add(shoppingCartVo);
            }
            return WebApiResult.ok(shoppingCartVos);
        } catch (Exception e) {
            e.printStackTrace();
            return WebApiResult.error("添加购物车失败");
        }
    }

    @PostMapping("/deleteShoppingCart")
    @ApiOperation("删除购物车数据")
    public WebApiResult deleteShoppingCart(@ApiParam(name = "gwid",value = "购物id")@RequestParam("gwid") int gwid){
        try {
            ygShoppingcartService.removeById(gwid);
            return WebApiResult.ok("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return WebApiResult.error("删除失败");
        }
    }
}
